const host = 'http://localhost:3000'

function back() {
    location.href = './blog.html'
}

function save() {
    const id = new URLSearchParams(location.search).get('id')
    const headline = $('input[name=headline]').val()
    console.log(headline);
    
    const digest = $('input[name=digest]').val()
    
    const content = $('input[name=content]').val()    

    const classify = $('input[name=classify]').val()    

    const author = $('input[name=author]').val()    
    

    if (id) {
        axios.put(`${host}/blog/${id}`,{headline,digest,content,classify}).then(res =>{
            if (res.data.code === 1000) {
                alert('博客修改成功！')
                location.href = './blog.html'
            }
        }).catch(error=>{
            console.error('更新博客发生错误：',error);
            
        })

    }else{
        axios.post(`${host}/blog`,{headline,digest,content,classify,author}).then(res =>{
            if (res.data.code === 1000) {
                alert('博客添加成功！')
                location.href = './blog.html'
            }
        }).catch(error=>{
            console.error('添加博客发生错误：',error);
            
        })

    }
}

$(function () {
    const id = new URLSearchParams(location.search).get('id')

    if (id) {
        axios.get(`${host}/blog/${id}`).then(res =>{
            const blog = res.data.data

            $('input[name=headline]').val(blog.headline)
            $('input[name=digest]').val(blog.digest)
            $('input[name=content]').val(blog.content)
            $('input[name=classify]').val(blog.classify)
            $('input[name=author]').val(blog.author)
            $('input[name=creatTime]').val(blog.creatTime)
        }).catch(error =>{
            console.error('获取博客详情发生错误：',error);
            
        })
    }
})